//
//  ByShapeDrawHandle.m
//  laserdevice_ios
//
//  Created by pro on 16/11/26.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByShapeDrawHandle.h"

#import "ByShapeBaseView.h"

@implementation ByShapeDrawHandle

+ (void)drawShapeWithShapeModels:(NSArray *)models {
    
    for (ByShapeModel *model in models) {
        // 非立方体
        if (model.mode != ByShapeModeCuboids) {
            
            [model adjustLabelsFrame];
            [self drawNormalShape:model];
        } else {
            [model adjustLabelsFrame];
            [self drawCuboidsShpae:model];
        }
    }
}

// 非立方体
+ (void)drawNormalShape:(ByShapeModel *)model {
    
    
    // 1.编辑触摸点
    if (model.selected) {
        UIBezierPath *editPath = [[UIBezierPath alloc] init];
        for (NSValue *pointValue in model.points) {
            CGPoint point = [pointValue CGPointValue];
            [editPath appendPath:[UIBezierPath bezierPathWithArcCenter:point radius:kPointDiameter / 4.0 startAngle:0.0 endAngle:2 * M_PI clockwise:YES]];
            
        }
        model.pointView.shapeLayer.path = editPath.CGPath;
    } else {
        model.pointView.shapeLayer.path = [UIBezierPath new].CGPath;
    }
    
    // 高亮线
    UIBezierPath *highLightPath = [[UIBezierPath alloc] init];
    [highLightPath moveToPoint:[[model.highLightPoints firstObject] CGPointValue]];
    [highLightPath addLineToPoint:[[model.highLightPoints lastObject] CGPointValue]];
    model.highLightLineView.shapeLayer.path = highLightPath.CGPath;

    
    // 2.线条
    if (model.points.count >= 2) {
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path moveToPoint:[[model.points firstObject] CGPointValue]];
        
        // 找到除去第一个点以外的所有点
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, model.points.count - 1)];
        [model.points enumerateObjectsAtIndexes:indexSet
                                        options:0
                                     usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop) {
                                         
                                         [path addLineToPoint:[pointValue CGPointValue]];
                                     }];
        
        // 除了线段和角度，其他的需要关闭路径
        if (model.mode != ByShapeModeLine && model.mode != ByShapeModeAngle) {
            [path closePath];
        }
        
        model.lineView.shapeLayer.path = path.CGPath;
    }
    
    
    // 角度线
    if (model.mode == ByShapeModeAngle) {
        UIBezierPath *anglePath = [[UIBezierPath alloc] init];
        CGPoint center = [model.points[1] CGPointValue];
        CGPoint point1 = [model.points[0] CGPointValue];
        CGFloat angle1 = 0;
        if (point1.x >= center.x && point1.y >= center.y) {
            angle1 = atan((point1.y - center.y) / (point1.x - center.x));
        }
        if (point1.x >= center.x && point1.y < center.y) {
            angle1 = atan((point1.y - center.y) / (point1.x - center.x));
        }
        if (point1.x < center.x && point1.y >= center.y) {
            angle1 = atan((point1.y - center.y) / (point1.x - center.x)) - M_PI;
        }
        if (point1.x < center.x && point1.y < center.y) {
            angle1 = -(atan((point1.x - center.x) / (point1.y - center.y)) + M_PI / 2);
        }
        CGPoint point2 = [model.points[2] CGPointValue];
        CGFloat angle2 = 0;
        if (point2.x >= center.x && point2.y >= center.y) {
            angle2 = atan((point2.y - center.y) / (point2.x - center.x));
        }
        if (point2.x >= center.x && point2.y < center.y) {
            angle2 = atan((point2.y - center.y) / (point2.x - center.x));
        }
        if (point2.x < center.x && point2.y >= center.y) {
            angle2 = atan((point2.y - center.y) / (point2.x - center.x)) - M_PI;
        }
        if (point2.x < center.x && point2.y < center.y) {
            angle2 = -(atan((point2.x - center.x) / (point2.y - center.y)) + M_PI / 2);
        }
        
        [anglePath addArcWithCenter:center radius:10 startAngle:angle1 endAngle:angle2  clockwise:YES];
    
        model.angleView.shapeLayer.path = anglePath.CGPath;
    }
}
// 立方体
+ (void)drawCuboidsShpae:(ByShapeModel *)model {
    
    if (model.selected) {
        // 编辑触摸点
        UIBezierPath *editPath = [[UIBezierPath alloc] init];
        for (NSValue *pointValue in model.cuboidsControllPoints) {
            CGPoint point = [pointValue CGPointValue];
            [editPath appendPath:[UIBezierPath bezierPathWithArcCenter:point radius:kPointDiameter / 4.0 startAngle:0.0 endAngle:2 * M_PI clockwise:YES]];
            
        }
        model.pointView.shapeLayer.path = editPath.CGPath;
    } else {
        model.pointView.shapeLayer.path = [UIBezierPath new].CGPath;
    }
    
    
    // 高亮线
    UIBezierPath *highLightPath = [[UIBezierPath alloc] init];
    [highLightPath moveToPoint:[[model.highLightPoints firstObject] CGPointValue]];
    [highLightPath addLineToPoint:[[model.highLightPoints lastObject] CGPointValue]];
    model.highLightLineView.shapeLayer.path = highLightPath.CGPath;

    
    // 绘制线
    UIBezierPath *path = [[UIBezierPath alloc] init];
    // 绘制上面
    [path moveToPoint:[[model.cuboidsDrawUpPoints firstObject] CGPointValue]];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, model.cuboidsDrawUpPoints.count - 1)];
    [model.cuboidsDrawUpPoints enumerateObjectsAtIndexes:indexSet
                                                 options:0
                                              usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop) {
                                     
                                                  [path addLineToPoint:[pointValue CGPointValue]];
                                              }];
    [path closePath];
    
    // 绘制下面
    [path moveToPoint:[[model.cuboidsDrawDownPoints firstObject] CGPointValue]];
    indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, model.cuboidsDrawUpPoints.count - 1)];
    [model.cuboidsDrawDownPoints enumerateObjectsAtIndexes:indexSet
                                                   options:0
                                                usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop) {
                                                  
                                                    [path addLineToPoint:[pointValue CGPointValue]];
                                                }];
    [path closePath];
    
    // 连接上下面
    indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, model.cuboidsDrawUpPoints.count)];
    [model.cuboidsDrawUpPoints enumerateObjectsAtIndexes:indexSet
                                                 options:0
                                              usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop) {
                                                  
                                                  [path moveToPoint:[pointValue CGPointValue]];
                                                  NSValue *pointValue2 = model.cuboidsDrawDownPoints[idx];
                                                  [path addLineToPoint:[pointValue2 CGPointValue]];
                                              }];
    
    model.lineView.shapeLayer.path = path.CGPath;
    
}

@end
